<?php
/**
 * @author jonathan poelen <jonathan.poelen@gmail.com>
 * @version 1.0: jo_link_noir
 * @date 04-06-2009
 */
abstract class SW_Structure_DAO_Player extends SW_Structure_DAO
{
	/**
	 * Ajoute une liste de construction et retourne son id
	 * @param int $id_structure
	 * @param int $level
	 * @param int $duration
	 * @param int $time_end
	 * @param int $id_planet
	 * @return int
	 */
	protected function insertList($id_structure, $level, $duration, $time_end, $id_planet)
	{
		$method = $this->nameMethodList(__METHOD__);
		if (!isset(self::$queries[$method]))
			$this->prepare($method, 'INSERT INTO '.$this->tableList.'('.$this->refPlanet.', '.$this->refStructure.', level, duration, time_end) VALUES(?,?,?,?,?)');

		self::$queries[$method]->execute(
			array($id_planet, $id_structure, $level, $duration, $time_end)
		);

		return (int)$this->pdo->lastInsertId();
	}

	/**
	 * Ajoute une $id_user et retourne son id d'enregistrement
	 * @param int $id_planet
	 * @param int $group
	 * @param int $level
	 * @return int
	 */
	protected function insertElement($id_planet, $group, $level)
	{
		$method = $this->nameMethodStructure(__METHOD__);
		if (!isset(self::$queries[$method]))
			$this->prepare($method, 'INSERT INTO '.$this->tableStructure.'('.$this->refPlayer.', `group`, level) VALUES(?,?,?)');

		self::$queries[$method]->execute(
			array($id_planet, $group, $level)
		);

		return (int)$this->pdo->lastInsertId();
	}

	/**
	 * @param int $id_user
	 * @return array : {SW_Structure_Element, ...}
	 */
	public function selectAll($id_user)
	{
		$sth = $this->pdo->query(
			'SELECT id, `group`, level, '.$this->refPlayer.' as ref_player FROM '.$this->tableStructure.' WHERE '.$this->refPlayer.'='.((int)$id_user),
			PDO::FETCH_OBJ
		);

		$elements = array();
		while ($e = $sth->fetchObject())
			$elements[] = $this->newElement($e->id, $e->group, $e->level, $e->ref_player);

		return $elements;
	}

	/**
	 * @param int $id_user
	 * @return array : {ref_planet => {SW_Structure_List, ...}, ...}
	 */
	public function selectListAll($id_user)
	{
		$sth = $this->pdo->query(
			'SELECT _l.id as id, _l.'.$this->refStructure.' as ref_structure, _l.'.$this->refPlanet.' as ref_planet, _l.level as level, duration, time_end FROM '.$this->tableList.' as _l
				JOIN '.$this->tableStructure.' as _s
					ON _l.'.$this->refStructure.' = _s.id
				WHERE _s.'.$this->refPlayer.'='.$id_user,
			PDO::FETCH_OBJ
		);

		$lists = array();
		while ($l = $sth->fetchObject())
		{
			$p = (int)$l->ref_planet;
			$lists[$p][] = $this->newList($l->id, $l->ref_structure, $l->level, $l->duration, $l->time_end, $p);
		}

		return $lists;
	}

	/**
	 * Crée la table de la structure et celle de la list
	 * @param int $levelGroup = 4
	 * @param int $levelSize = 8
	 */
	public function createTable($levelGroup = 4, $levelSize = 8)
	{
		return $this->pdo->query(
			'CREATE TABLE IF NOT EXISTS '.$this->tableStructure.' (
				`id` int(11) NOT NULL AUTO_INCREMENT,
				'.$this->refPlayer.' int(11) NOT NULL,
				`group` tinyint('.((int)$levelGroup).') NOT NULL,
				`level` tinyint('.((int)$levelSize).') unsigned NOT NULL,
				PRIMARY KEY (`id`),
				KEY `ref_planet` ('.$this->refPlayer.'),
				KEY `group` (`group`)
			) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci AUTO_INCREMENT=1 ;'
		) && $this->pdo->query(
			'CREATE TABLE IF NOT EXISTS '.$this->tableList.' (
				`id` int(11) NOT NULL AUTO_INCREMENT,
				'.$this->refPlanet.' int(11) NOT NULL,
				'.$this->refStructure.' int(11) NOT NULL,
				`level` tinyint('.((int)$levelSize).') unsigned NOT NULL,
				`duration` mediumint(9) NOT NULL,
				`time_end` int(11) NOT NULL,
				PRIMARY KEY (`id`),
			KEY `ref_planet` ('.$this->refPlanet.'),
			KEY `ref_structure` ('.$this->refStructure.')
			) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci AUTO_INCREMENT=1 ;'
		);
	}
}